package com.ruoyi.erecord.samplebox.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erecord.samplebox.domain.vo.SampleboxVo;
import com.ruoyi.erecord.samplebox.domain.bo.SampleboxBo;
import com.ruoyi.erecord.samplebox.service.ISampleboxService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 样本框
 *
 * @author cc
 * @date 2023-04-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erecord/samplebox")
public class SampleboxController extends BaseController {

    private final ISampleboxService iSampleboxService;

    /**
     * 查询样本框列表
     */
    @SaCheckPermission("erecord:samplebox:list")
    @GetMapping("/list")
    public TableDataInfo<SampleboxVo> list(SampleboxBo bo, PageQuery pageQuery) {
        return iSampleboxService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出样本框列表
     */
    @SaCheckPermission("erecord:samplebox:export")
    @Log(title = "样本框", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SampleboxBo bo, PageQuery pageQuery, HttpServletResponse response) {
    	pageQuery.setPageSize(10000);
        TableDataInfo<SampleboxVo> list = iSampleboxService.queryPageList(bo, pageQuery);
        ExcelUtil.exportExcel(list.getRows(), "样本框", SampleboxVo.class, response);
    }

    /**
     * 获取样本框详细信息
     *
     * @param sampleboxCode 主键
     */
    @SaCheckPermission("erecord:samplebox:query")
    @GetMapping("/{sampleboxCode}")
    public R<SampleboxVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long sampleboxCode) {
        return R.ok(iSampleboxService.queryById(sampleboxCode));
    }

    /**
     * 新增样本框
     */
    @SaCheckPermission("erecord:samplebox:add")
    @Log(title = "样本框", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SampleboxBo bo) {
        return toAjax(iSampleboxService.insertByBo(bo));
    }

    /**
     * 修改样本框
     */
    @SaCheckPermission("erecord:samplebox:edit")
    @Log(title = "样本框", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SampleboxBo bo) {
        return toAjax(iSampleboxService.updateByBo(bo));
    }

    /**
     * 删除样本框
     *
     * @param sampleboxCodes 主键串
     */
    @SaCheckPermission("erecord:samplebox:remove")
    @Log(title = "样本框", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sampleboxCodes}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] sampleboxCodes) {
        return toAjax(iSampleboxService.deleteWithValidByIds(Arrays.asList(sampleboxCodes), true));
    }
}
